import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { BullModule } from '@nestjs/bull';
import { configuration } from './config';

import { SharedModule } from './shared/shared.module';
import { CosModule } from './modules/cos/cos.module';
import { AuthModule } from './modules/auth/auth.module';
import { AdminModule } from './modules/admin/admin.module';
import { WeChatModule } from './modules/wechat/wechat.module';
import { WSModule } from './modules/ws/ws.module';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [configuration],
      envFilePath: [`.env.${process.env.NODE_ENV}`, '.env'],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (config: ConfigService) => ({
        autoLoadEntities: true,
        type: config.get<any>('database.type'),
        host: config.get<string>('database.host'),
        port: config.get<number>('database.port'),
        username: config.get<string>('database.username'),
        password: config.get<string>('database.password'),
        database: config.get<string>('database.database'),
        synchronize: config.get<boolean>('database.synchronize'),
        timezone: config.get('database.timezone'),
      }),
    }),
    // Bull 是一个基于 Redis 的队列库
    BullModule.forRoot({}),
    SharedModule,
    CosModule,
    AuthModule,
    AdminModule,
    WeChatModule,
    WSModule,
  ],
})
export class AppModule {}
